package com.ideeli.webextractor.service.old;

import static com.ideeli.webextractor.utils.Constants.BASE_URL;
import static com.ideeli.webextractor.utils.Constants.INGRAM_PASS;
import static com.ideeli.webextractor.utils.Constants.INGRAM_USER;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import com.ideeli.webextractor.domain.ProductDetail;
import com.ideeli.webextractor.domain.SkuVpn;
import com.ideeli.webextractor.utils.Utility;

public class IngramExtractorService
{
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	private List<String> skusList;
	private static int i;
	private Properties props;
	private ExtractProdDetailsService extractProdDetails;
	private List<ProductDetail> productDetailList;
	
	


	@Before
	public void setUp() throws Exception
	{
		
		
		props = new Properties();
		props.load(new FileInputStream("prop/app.properties"));

		File fExeFile = new File("C:\\Users\\spaida\\prog\\firefox.exe");
		FirefoxBinary fireFoxBinary = new FirefoxBinary(fExeFile);
		driver = new FirefoxDriver(fireFoxBinary, null);
	
//		driver = new FirefoxDriver();
		baseUrl = props.getProperty(BASE_URL);

		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		skusList = new ArrayList<String>();
		extractProdDetails = new ExtractProdDetailsService();
		
		productDetailList= new ArrayList<ProductDetail>();

	}

	@Test
	public void testIngFinalWithPass() throws Exception
	{

		System.out.println("baes url..");
		driver.get(baseUrl + "/");

		System.out.println("username..");
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys(props.getProperty(INGRAM_USER));
		System.out.println("password..");
		driver.findElement(By.id("password")).clear();
		driver.findElement(By.id("password")).sendKeys(props.getProperty(INGRAM_PASS));
		System.out.println("click login..");
		driver.findElement(By.id("loginBtn")).click();
		System.out.println("prodcuct activate click..");
		driver.findElement(By.id("product-activate")).click();
		Thread.sleep(1000 * 2);
		System.out.println("qtp_showHideFacetTrigger..");
		driver.findElement(By.cssSelector("#qtp_showHideFacetTrigger > div.arrow-container")).click();

		try
		{
			System.out.println("stock1..");
			driver.findElement(By.id("qtp_SelectedStatusValuesList")).click();

		} catch (Exception e)
		{
			System.out.println("Exception block..");
			System.out.println("stock2..");
			driver.findElement(By.id("qtp_SelectedStatusValuesList3")).click();

		}

		System.out.println("stock srch..");
		driver.findElement(By.id("qtp_facet_actions_btn")).click();
		Thread.sleep(1000 * 3);		

//		new Select(driver.findElement(By.id("topPagingSelect"))).selectByVisibleText("30");
//		System.out.println("select 30..");
//		driver.findElement(By.cssSelector("option[value=\"30\"]")).click();
//
//		Thread.sleep(1000 * 4);
		System.out.println("high to low..");
		driver.findElement(By.id("topSortSelect")).click();
		new Select(driver.findElement(By.id("topSortSelect"))).selectByVisibleText("Price, High to Low");
		driver.findElement(By.cssSelector("option[value=\"1\"]")).click();

		// List<WebElement> prodNumbers = driver.findElements(By
		// .className("prod-number-container"));

		// System.out.println("Got elements link..");

		int i = 0;
		// for(WebElement prodNumber: prodNumbers)
		// {
		// System.out.println("webElement  : "+i+"  : "+ prodNumber.getText());
		// i++;
		// }

		Thread.sleep(1000 * 2);
		// List<WebElement> pricingInfo =
		// driver.findElements(By.className("pricing-info"));
		//
		// i = 0;
		// for (WebElement price : pricingInfo)
		// {
		// System.out.println("price  : " + i + "  : " + price.getText());
		// i++;
		// }

		try
		{
//			while (true)
//			{
				List<WebElement> topDetails = driver.findElements(By.className("top-details"));
				recurIterate(topDetails);
				System.out.println("*************clicking.. next..*****************");
//				driver.findElement(By.id("next")).click();
				Thread.sleep(1000 * 2);

//			}
			
			System.out.println("All links visited Gracefully!!");
			
		} catch (NoSuchElementException nosuchele)
		{
			System.out.println("All links visited!!");
		}
		
		
		
		printProductDetail();

		while (true)
		{
			Thread.sleep(1000 * 60);
		}

	}

	private void printProductDetail()
	{
		for(ProductDetail productDetail: productDetailList)
		{
			
			System.out.println(productDetail);
			System.out.println("\n*****info print*****\n");
			
			Utility.printMap(productDetail.getBaseProps());
			
			
			Utility.printMap(productDetail.getExtPropMap());
			
			for(String str: productDetail.getExtPropList())
			{
				System.out.println(str);
			}
			
		}
		
	}

	private void recurIterate(List<WebElement> topDetails) throws Exception
	{

		for (WebElement prodDetail : topDetails)
		{
			String prodDetailStr = prodDetail.getText();
			// System.out.println("topDetail  : " + i + "  : " + prodDetailStr);
			SkuVpn sku = Utility.extractSKU(prodDetailStr);

			if (!skusList.contains(sku))
			{
				i++;
				skusList.add(sku.toString());

				WebElement clickProd = prodDetail.findElement(By.className("tooltip-clickable"));
				System.out.println("****Prod no : " + i);
				System.out.println("clickProd  : " + clickProd.getText());
				clickProd.click();
				Thread.sleep(1000 * 2);
				// **************************** CALL Extract Prod Details
				// Service class************************
				ProductDetail productDetail = extractProdDetails.extract(driver);
				productDetailList.add(productDetail);
				
				
				 Thread.sleep(1000 * 3);
				 driver.navigate().back();
				 Thread.sleep(1000 * 3);
				 List<WebElement> topDetailsRec =
				 driver.findElements(By.className("top-details"));
				 
				 if(i< 4)
					 recurIterate(topDetailsRec);
				
				
				break;
			}

		}

	}

	private boolean isElementPresent(By by)
	{
		try
		{
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e)
		{
			return false;
		}
	}

	@After
	public void tearDown() throws Exception
	{
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString))
		{
			fail(verificationErrorString);
		}
	}
	


}
